(function(){
	var cssText = "";
	var showHeight = window.innerHeight-100;
	var htmlTemp='<div id="console" >\
		<button class="console-button" id="console-hander">开/关</button>\
		<div id="console-panel">\
			<div class="console-panel-hd">\
				日志记录详情\
			</div>\
			<div class="console-panel-content" style="height:'+showHeight+'px">\
				<ul id="console-panel-bd" class="console-panel-bd">\
				</ul>\
			</div>\
			<div class="console-panel-ft">\
				<button class="console-button" id="console-clear">清除</button>\
			</div>\
		</div>\
	</div>'
	
	var css='<style type="text/css">'+cssText+'</style>';
	
	
	//拖动基本参数
	var orgP = {
			y : 0
		}, aimP = {
			y : 0
		}, moveLength = 0,forwardInfo=0;
 	var hander,panel,panelBd,clearHander;
	//保存系统控制台引用
	var systemConsole = window.console;
	
	 function $(str){
	 	return document.getElementById(str);
	 }
	 
	 /**
	  * 通过一个字符串创建一个对象
	  */
	 function createElement(str){
	 	var temp = document.createElement("div");
	 	temp.innerHTML=str;
	 	return temp.firstChild;
	 }
	 
 	function init(){
 		initConsolePanel();
 		initEvent();
 	}
 	/**
 	 * 初始化控制台面板
 	 */
 	function initConsolePanel(){
 		
		document.head.appendChild(createElement(css))
		document.body.appendChild(createElement(htmlTemp));
	
		hander = $("console-hander");
		panel = $("console-panel");
		panelBd = $("console-panel-bd");
		clearHander = $("console-clear");
 	
 	}
 	/**
 	 * 初始化控制台基本事件
 	 */
 	function initEvent(){
 		//绑定基本事件
 		hander.addEventListener("click",function(e){
 			var isOpen = panel.isOpen;
 			if(isOpen){
 				panel.style.display="none";
 				panel.isOpen=false;
 				
 			}else{
 				panel.style.display="block";
 				panel.isOpen=true;
 				window.scrollTo(0,0);
 			}
 		},true);
 		clearHander.addEventListener("click",function(e){
 			panelBd.innerHTML="";
 			touchBd.translate(0);
 		},true);
 		
 		/*panelBd.addEventListener("click",function(e){
 			var target = e.target;
 			if(target.className=="info"){
 				var isOpen = target.isOpen;
	 			if(isOpen){
	 				target.style.cssText="max-height:40px";
	 				target.isOpen=false;
	 				
	 			}else{
	 				target.style.cssText="max-height:400px";
	 				target.isOpen=true;
	 			}
 				
 			}
 		});*/
 		
 		
 		panelBd.addEventListener("touchstart",touchBd.touchStar, false);
		panelBd.addEventListener("touchmove",touchBd.touchMove, false);
		panelBd.addEventListener("touchend", touchBd.touchEnd, false);
 		
 	}
		
	
	
 	var touchBd = {
 		leftOrRight : function(orgP, aimP) {
			var forward = {};
			forward.length = aimP.y - orgP.y;
			forward.y = aimP.y - orgP.y;
	
			if (forward.length > 0) {
				forward.flag = 1;
			} else if (forward.length < 0) {
				forward.flag = -1;
			} else {
				forward.flag = 0;
			}
			forward.fill = forward.length * forward.flag;
			return forward;
		},
 		touchStar:function(event){
 			var touch = event.touches[0];
			// 设置源对象位置
			orgP.y = touch.screenY;
 		},
 		touchMove:function(event){
 			event.preventDefault();

			var touch = event.touches[0];
			aimP.y = touch.screenY;
			forwardInfo = touchBd.leftOrRight(orgP, aimP);
			var move = moveLength + forwardInfo.length;
	
			touchBd.translate(move);
			
 		},
 		touchEnd:function(event){
 			moveLength = moveLength + forwardInfo.length;
			//计算移动
 			if(Math.abs(moveLength)>panelBd.scrollHeight-showHeight){
 				moveLength = -(panelBd.scrollHeight-showHeight);
 			}
 			if(moveLength>0){
 				moveLength=0;
 			}
			
			touchBd.translate(moveLength);
			
 		},translate :function(y) {
			panelBd.style.webkitTransform = 'translate3d(' + 0+ 'px,' + y+ 'px,0px)';
		}
 	}
 	
		
 	//控制台面板操作
 	var panelControl = {
 		addItemToPanel:function(str){
 			var liNode;
 			var outStr = "";
 			var argu = [];
 			for(var i=0;i<arguments.length;i++){
 				argu.push(arguments[i]);
 			}
 			if(argu.length>1){
 				outStr = JSON.stringify(argu[0])+"<br/>"+JSON.stringify(argu.slice(1));
 			}else if(argu.length==1){
 				if(typeof argu[0]=="string"){
 				
	 				outStr = argu[0];
 				}else{
	 				outStr = JSON.stringify(argu[0]);
 				}
 			}
 			var tempLi='<li class="item-info">\
					<div class="info">\
						'+outStr+'\
					</div>\
				</li>'
						
 			
 			liNode = createElement(tempLi);
 			panelBd.appendChild(liNode);
 			if(panelBd.scrollHeight-showHeight>0){
	 			touchBd.translate(-(panelBd.scrollHeight-showHeight));
 			}
 		}
 	}
	
	var console = {
		log : function(){
			systemConsole.log.apply(systemConsole,arguments);
			panelControl.addItemToPanel.apply(panelControl,arguments);
		},
		debug:function(){
			systemConsole.debug.apply(systemConsole,arguments);
			panelControl.addItemToPanel.apply(panelControl,arguments);
		},
		info:function(){
			systemConsole.info.apply(systemConsole,arguments);
			panelControl.addItemToPanel.apply(panelControl,arguments);
		},
		warn:function(){
			systemConsole.warn.apply(systemConsole,arguments);
			panelControl.addItemToPanel.apply(panelControl,arguments);
		},
		error:function(){
			systemConsole.error.apply(systemConsole,arguments);
			panelControl.addItemToPanel.apply(panelControl,arguments);
		},
		dir:function(){
			systemConsole.dir.apply(systemConsole,arguments);
		},
		time:function(){
			systemConsole.time.apply(systemConsole,arguments);
		},
		timeEnd:function(){
			systemConsole.timeEnd.apply(systemConsole,arguments);
		}
	}
	
	window.console = console;
	
	//初始化控制台
 	init();
 	
 
})();